package com.ccnf.controller;

import com.ccnf.core.bean.Response;
import com.ccnf.core.bean.ResponseWithData;
import com.ccnf.model.entity.Authorization;
import com.ccnf.model.entity.UserApply;
import com.ccnf.model.enums.ApplyStatusEnum;
import com.ccnf.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户社交信息表 前端控制器
 * </p>
 *
 * @author developer
 * @since 2018-10-09
 */
@RestController
@RequestMapping("/v1/user")
@Api(value = "UserController", description = "用户相关API")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @ApiOperation("查看用户信息，如果不存在该用户信息则返回null")
    @GetMapping
    public ResponseWithData get() {
        return ResponseWithData.success(userService.findUserInfo(getTokenParams()));
    }

    @ApiOperation(value = "生成授权码信息")
    @PostMapping("auth")
    public ResponseWithData createAuth(@RequestBody Authorization auth) {
        return ResponseWithData.success(userService.createRoleAuth(getTokenParams(), auth));
    }

    @ApiOperation("录入用户申请信息")
    @PostMapping("apply")
    public Response userApply(@RequestBody UserApply apply) {
        userService.saveUserApply(apply, getTokenParams());
        return Response.success();
    }

    @ApiOperation("获取用户申请信息")
    @GetMapping("apply/info")
    public ResponseWithData userApply() {
        return ResponseWithData.success(userService.findUserApplyInfo(getTokenParams()));
    }

    @ApiOperation("用户申请列表")
    @GetMapping("apply/list")
    public ResponseWithData userApplyList(ApplyStatusEnum applyStatus) {
        return ResponseWithData.success(userService.listUserApply(applyStatus, getTokenParams()));
    }

    @ApiOperation("审核用户申请")
    @GetMapping("apply/audit")
    public Response userApplyAudit(Long applyId, ApplyStatusEnum applyStatus) {
        userService.handleUserApplyAudit(applyId, applyStatus);
        return Response.success();
    }

    @ApiOperation("授权列表")
    @GetMapping("auth/list")
    public ResponseWithData authList() {
        return ResponseWithData.success(userService.listAuth(getTokenParams()));
    }

    @ApiOperation("解除授权")
    @DeleteMapping("auth/relieve")
    public Response relieveAuth(String authCode) {
        userService.relieveAuth(authCode, getTokenParams());
        return Response.success();
    }
}